package com.nemlistapp;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class CreateUserActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_create_user);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.create_user, menu);
		return true;
	}
	
	public void createUser(View view) throws InterruptedException
	{
		EditText firstName = (EditText)findViewById(R.id.createFirstNameField);
		EditText lastName = (EditText)findViewById(R.id.createLastNameField);
		EditText userName = (EditText)findViewById(R.id.createUsernameField);
		EditText email = (EditText)findViewById(R.id.createEmailField);
		EditText password = (EditText)findViewById(R.id.createPasswordField);
		EditText passwordAgain = (EditText)findViewById(R.id.createPasswordAgainField);
		int createUserState = -1;
		
		boolean success = true;
		
		if(Connection.getInstance().checkNetwork(this))
		{
		Pattern pattern = Pattern.compile(".+@.+\\.[a-z]+");
		Matcher matcher = pattern.matcher(email.getText().toString());
		boolean matchFound = matcher.matches();
		
		if(!matchFound)
		{
			success = false;
			email.setError(getString(R.string.wrongDK));
		}
		else
		{
				//Needs to call backend and check if email already exists
		}
		
		if(firstName.length() == 0)
		{
			success = false;
			firstName.setError(getString(R.string.wrongDK));
		}
		
		if(lastName.length() == 0)
		{
			success = false;
			lastName.setError(getString(R.string.wrongDK));
		}
		
		if(userName.length() == 0)
		{
			success = false;
			userName.setError(getString(R.string.wrongDK));
		}
				
		//if(password.getText().toString() != passwordAgain.getText().toString())
		if(!password.getText().toString().equalsIgnoreCase(passwordAgain.getText().toString()))
		{
			Log.d("debug","password: "+password.getText().toString());
			Log.d("debug","passwordagain: "+passwordAgain.getText().toString());
			success = false;
			passwordAgain.setError(getString(R.string.wrongDK));
		}
		
		if(success)
		{
			String fname = firstName.getText().toString();
			String lname = lastName.getText().toString();
			String uname = userName.getText().toString();
			String mail = email.getText().toString();
			String pass = password.getText().toString();
    		
    		CreateUserFetcher createUserFetcher = new CreateUserFetcher(this);
    		createUserFetcher.execute(fname,lname,uname,mail,pass);

    		createUserState = States.getInstance().getCreateUserState();
    		
    		/*while(createUserState == -1 && createUserFetcher.getStatus() != AsyncTask.Status.FINISHED)
    		{
    			createUserState = States.getInstance().getCreateUserState();
    		}*/
    		
    		//TextView loginText = (TextView)findViewById(R.id.loginText);
    		
    		//createUserState = 2;
    		/*switch (createUserState) {
	            case 1: 
	            	//loginText.setText("Brugeren er oprettet login her");
		            Intent intent = new Intent(this, LoginActivity.class);
		            States.getInstance().setCreateUserState(-1);
		        	startActivity(intent);
		        	break;
	            case 2: 
	            	userName.setError("Brugernavnet er brugt");
	            	States.getInstance().setCreateUserState(-1);
	            	break;
	            case 3: 
	            	email.setError("Email er brugt");
	            	States.getInstance().setCreateUserState(-1);
	            	break;
	            default: 
    		}*/
            	
		}	
		
		}
		
		else
		{
			success = false;
			Toast.makeText(this, "Ingen internet forbindelse", Toast.LENGTH_LONG).show();
		}
		
		
		/*if(success)
		{
			Intent intent = new Intent(this, LoginActivity.class);
			startActivity(intent);
		}*/
	}
	

}
